﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;

namespace EditWikiPage.WikiPageContentHandler
{
    public class WikiContentPopulator
    {
        /// <summary>
        /// get theccopy of the content(xml file) on to the memory
        /// </summary>
        /// <param name="filenamePath">In memory XMl Document </param>
        /// <returns></returns>
        private XmlDocument GetDataFile(string filenamePath)
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(filenamePath);
                return xmlDocument;
            }
            catch (Exception exception)
            {
                //Error Retrieving XmlDocument
                return null;
            }
            return null;
        }

        public string ReadContent(XmlDocument content)
        {
            using (SPSite site = SPContext.Current.Site)
            {
                foreach (XmlNode xmlWikiItem in content.SelectNodes("//WikiPage"))
                {
                    using (SPWeb web = site.OpenWeb(xmlWikiItem.Attributes["RelativeUrl"].Value))
                    {
                        foreach (XmlNode wikiContent in xmlWikiItem.SelectNodes("Content"))
                        {
                            string relativeUrl = xmlWikiItem.Attributes["RelativeUrl"].Value;

                            try
                            {
                                switch (xmlWikiItem.Attributes["Type"].Value)
                                {
                                    case "WikiPage":
                                        {
                                            break;
                                        }
                                    case "WebPart":
                                        {
                                          //  AddWebPart(relativeUrl, webPart);
                                            break;
                                        }
                                    case "HTML":
                                        {
                                            break;
                                        }
                                    case "Image":
                                        {
                                            break;
                                        }
                                }
                            }
                             catch (Exception)
                            {
                                throw;
                            }
                        }
                    }
                }
            }
            return null;
        }

        public static SPList GetSitePageLibrary(SPWeb spWeb)
        {
            SPList wikiList;

            try
            {
                string relativeUrl = spWeb.ServerRelativeUrl;
                if (relativeUrl == "/")
                {
                    relativeUrl = "/SitePages";

                }
                else
                {
                    relativeUrl = relativeUrl + "/SitePages";
                }
                wikiList = spWeb.GetList(relativeUrl);
            }
            catch (Exception)
            {
                throw;
            }
            if ((wikiList != null) && (wikiList.BaseTemplate != SPListTemplateType.WebPageLibrary))
            {
                wikiList = null;
            }
            return wikiList;
        }

        private static void EnsuresitePageLibrary(SPWeb web, SPList sitePageLibrary)
        {
            if (sitePageLibrary == null)
            {
                sitePageLibrary = web.Lists.EnsureSitePagesLibrary();
            }
            if (sitePageLibrary == null)
            {
                throw new SPException(SPResource.GetString("ListGone", new object[0]));
            }
            if (sitePageLibrary.BaseTemplate != SPListTemplateType.WebPageLibrary)
            {
                throw new SPException(SPResource.GetString("OnlyWikiLibraries", new object[0]));
            }
            if (sitePageLibrary.ParentWeb != web)
            {
                throw new SPException(SPResource.GetString("WikiNotInWebException", new object[0]));

            }
        }
    }
}
